package oa.service.impl;

import java.util.Collection;
import java.util.Date;

import javax.annotation.Resource;

import oa.dao.ReplyDao;
import oa.domain.forum.Forum;
import oa.domain.forum.Reply;
import oa.domain.forum.Topic;
import oa.domain.page.PageBean;
import oa.service.ReplyService;
import oa.util.HQLHelper;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service("replyService")
public class ReplyServiceImpl implements ReplyService{

	@Resource(name="replyDao")
	private ReplyDao replyDao;
	@Override
	@Transactional(readOnly=false)
	public void saveReply(Reply reply) {
		// TODO Auto-generated method stub
		this.replyDao.saveEntity(reply);
		
		Topic topic = reply.getTopic();//持久对象
		Forum forum = topic.getForum();
		forum.setArticleCount(forum.getArticleCount()+1);//版块的文章数量加1
		topic.setReplyCount(topic.getReplyCount()+1);//回复数量加1
		topic.setLastReply(reply);//主题的最后一个回复为当前回复
		topic.setLastUpdateTime(new Date());//回复对应的主题的最后更新时间为回复时间
		
	}

	@Override
	public Reply getReplyById(Long id) {
		// TODO Auto-generated method stub
		return this.replyDao.getEntryById(id);
	}

	@Override
	@Transactional(readOnly=false)
	public void upateReply(Reply reply) {
		// TODO Auto-generated method stub
		this.replyDao.updateEntry(reply);
	}

	@Override
	@Transactional(readOnly=false)
	public void deleteReply(Long id) {
		// TODO Auto-generated method stub
		this.replyDao.deleteEntry(id);
	}

	@Override
	public Collection<Reply> getReplyByTopic(Topic topic) {
		// TODO Auto-generated method stub
		return this.replyDao.getReplyByTopic(topic);
	}

	@Override
	public PageBean getPageBean(int currentPage, Topic topic) {
		// TODO Auto-generated method stub
		return this.replyDao.getPageBean(currentPage,topic);
	}

	@Override
	public PageBean getPageBean(HQLHelper hh, int currentPage) {
		// TODO Auto-generated method stub
		return this.replyDao.getPageBean(hh, currentPage);
	}
	

}
